﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GameScore : MonoBehaviour {

    public Text gameScore;
    public Text arrows;
    public Text highScore;
        
    private float score;
    private TargetHolder targetHolder;

    private void Awake()
    {
        targetHolder = GameObject.Find("TargetHolder").GetComponent<TargetHolder>();
        arrows.text = "Arrow:" + targetHolder.arrows.ToString();
        CheckHighScore();

        Target.OnDestroyedByArrow += OnHitTarget;
        TargetHolder.OnArrowDecrease += ReleaseArrow;
        TargetHolder.OnGameEnd += CompareHighestScore;
    }

    private void CheckHighScore() {
        if (PlayerPrefs.HasKey("HighScore"))
        {
            highScore.text = "Highest:" + PlayerPrefs.GetInt("HighScore").ToString();
        }
        else
        {
            highScore.text = "Highest:0";
        }
    }

    void CompareHighestScore()
    {
        if (PlayerPrefs.HasKey("HighScore"))
        {
            if(score > PlayerPrefs.GetInt("HighScore"))
            {
                PlayerPrefs.SetInt("HighScore", (int)score);
                highScore.text = "Highest:" + score.ToString();
            }

        }else
        {
            PlayerPrefs.SetInt("HighScore", (int)score);
            highScore.text = "Highest:" + score.ToString();
        }
    }

    void OnHitTarget()
    {
        score++;
        gameScore.text = "HitPoint:" + score.ToString();
    }

    void ReleaseArrow()
    {
        arrows.text = "Arrow:" + targetHolder.arrows.ToString();
    }


    private void OnDestroy()
    {
        Target.OnDestroyedByArrow -= OnHitTarget;
        BallControl.OnReleaseArrow -= ReleaseArrow;
        TargetHolder.OnGameEnd -= CompareHighestScore;
    }

    public float GetScore()
    {
        float s = score;
        return s;
    }


}
